﻿#include "RMIProgress.h"
#include <RMIDelegates.h>
#include "Components/TextBlock.h"
#include "RuntimeActor.h"
#include "Components/ProgressBar.h"
#include "Engine/StreamableManager.h"
#include "EKThreadManager.h"


FRMIProgress::FRMIProgress()
	: m_pLoadUI(nullptr)
	, m_nMeshTotalCount(-1)
	, m_nMeshReadIndex(0)
	, m_nChildrenTotalCount(-1)
	, m_nChildCreateIndex(0)
	, m_sPath(FString(TEXT("")))
{
	FRMIDelegates::OnImportProgressDelegate.Remove(m_OnImportProgressDelegateHandle);
	m_OnImportProgressDelegateHandle = FRMIDelegates::OnImportProgressDelegate.AddRaw(this, &FRMIProgress::UpdateProgressUI);
}

FRMIProgress::~FRMIProgress()
{
	FRMIDelegates::OnImportProgressDelegate.Remove(m_OnImportProgressDelegateHandle);
}

void FRMIProgress::Init(bool bShowUI, const FString& sFileName)
{
	m_sPath = sFileName;
	m_bShowBuiltInProgress = bShowUI;
	CreateProgressUIStart();
}

void FRMIProgress::UpdateProgressUI(ERMIImportProgressStage emStage, int32 nCurIndex, int32 nMaxIndex, const FString& sMeshName)
{
	/*if (emStage == ERMIImportProgressStage::Stage1_2_ReadMesh)
	{
		++m_nMeshReadIndex;
	}
	else if (emStage == ERMIImportProgressStage::Stage2_2_CreateSection)
	{
		++m_nChildCreateIndex;
	}*/

	if (m_bShowBuiltInProgress && IsLoadUIValid())
	{
		//打印进度
		UEnum* EnumPtr = FindObject<UEnum>(ANY_PACKAGE, TEXT("ERMIImportProgressStage"), true);
		FString SatgeStr(EnumPtr->GetNameByValue((int)emStage).ToString());
		FString strpr = FString::Printf(TEXT("%s: %d/%d %s"), *SatgeStr, nCurIndex, nMaxIndex, *sMeshName);
		ULogRMImport::Log(strpr, true, 100);

		//判断UI是否在界面上?
		if (m_pLoadUI->IsInViewport())
		{
			//百分比
			UProgressBar* ProgressBar_Load = Cast<UProgressBar>(m_pLoadUI->GetWidgetFromName("ProgressBar_Load"));
			UTextBlock* TextBlock_Progress = Cast<UTextBlock>(m_pLoadUI->GetWidgetFromName("TextBlock_Progress"));
			//加载说明
			UTextBlock* TextBlock_Content = Cast<UTextBlock>(m_pLoadUI->GetWidgetFromName("TextBlock_Content"));


			float fPercent = 0.f;
			if (emStage == ERMIImportProgressStage::Stage1_2_ReadMesh)
			{
				fPercent = (float)m_nMeshReadIndex / (float)(m_nMeshTotalCount) / 2;
			}
			else if (emStage == ERMIImportProgressStage::Stage2_2_CreateSection)
			{
				fPercent = (float)m_nChildCreateIndex / (float)(m_nChildrenTotalCount) / 2 + 0.5f;
			}

			//UpdateUI
			if (TextBlock_Content) {
				TextBlock_Content->SetText(FText::FromString(strpr));
			}
			if (ProgressBar_Load) {
				ProgressBar_Load->SetPercent(fPercent);
			}
			if (TextBlock_Progress) {
				TextBlock_Progress->SetText(FText::FromString(FString::FromInt(FMath::FloorToInt(fPercent * 100)) + TEXT("%")));
			}
		}
	}

	//广播全部加载完毕的条件 或者关系
	//1>section总数>0并且一定要创建完
	//2>section总数=0, FbxMesh/ObjMesh/IfcMesh数量>0并且要读完
	//3>section总数=0, FbxMesh/ObjMesh/IfcMesh数量=0
	if ( (m_nChildrenTotalCount > 0 && m_nChildCreateIndex == m_nChildrenTotalCount) ||
		(m_nMeshTotalCount > 0 && m_nMeshReadIndex == m_nChildrenTotalCount && m_nChildrenTotalCount == 0) ||
		(m_nMeshTotalCount == 0 && m_nChildrenTotalCount == 0)
		)
	{
		FRMIDelegates::OnImportCompleteDelegate.Broadcast(m_sPath);

		if (IsLoadUIValid())
		{
			m_pLoadUI->RemoveFromParent();
			m_pLoadUI = nullptr;
		}
	}
}

void FRMIProgress::CreateProgressUIStart()
{
	if (m_bShowBuiltInProgress)
	{
		//创建UI提示
		FStringAssetReference ref(RMI_BUILTIN_PROGRESUI_PATH);
		UObject* uoTmp = ref.ResolveObject();
		if (uoTmp == nullptr)
		{
			FStreamableManager* EKAssetLoader = GEKThread::GetStreamble().GetStreamableManager();
			if (EKAssetLoader)
			{
				uoTmp = EKAssetLoader->LoadSynchronous(ref, true);
			}
		}
		if (uoTmp)
		{
			m_pLoadUI = CreateWidget<UUserWidget>(GWorld->GetGameInstance(), Cast<UClass>(uoTmp));
			if (IsLoadUIValid())
			{
				m_pLoadUI->AddToViewport();

				//初始化UI
				UProgressBar* ProgressBar_Load = Cast<UProgressBar>(m_pLoadUI->GetWidgetFromName("ProgressBar_Load"));
				UTextBlock* TextBlock_Progress = Cast<UTextBlock>(m_pLoadUI->GetWidgetFromName("TextBlock_Progress"));
				UTextBlock* TextBlock_Content = Cast<UTextBlock>(m_pLoadUI->GetWidgetFromName("TextBlock_Content"));
				if (TextBlock_Content) {
					TextBlock_Content->SetText(FText::FromString(TEXT("正在初始化解析文件, 请等待")));
				}
				if (ProgressBar_Load) {
					ProgressBar_Load->SetPercent(0.f);
				}
				if (TextBlock_Progress) {
					TextBlock_Progress->SetText(FText::FromString(TEXT("")));
				}
			}
		}

		////同步创建UI并添加到视口上
		//FStringAssetReference StringRef(RMI_BUILTIN_PROGRESUI_PATH);
		//UObject* uoTmp = StringRef.ResolveObject();
		//if (uoTmp == nullptr)
		//{
		//	FStreamableManager* EKAssetLoader = GEKThread::GetStreamble().GetStreamableManager();
		//	if (EKAssetLoader)
		//	{
		//		uoTmp = EKAssetLoader->LoadSynchronous(StringRef, true);
		//	}
		//}
		//if (uoTmp)
		//{
		//	GetGameInstance
		//	m_pLoadUI = CreateWidget<UUserWidget>(GWorld, Cast<UClass>(uoTmp));
		//	m_pLoadUI->AddToViewport(0);
		//}
		/*FStringAssetReference StringRef(RMI_BUILTIN_PROGRESUI_PATH);
		TArray<FSoftObjectPath> ObjectPaths;
		ObjectPaths.Add(StringRef);
		GEKThread::GetStreamble().SetObjectPath(ObjectPaths);
		GEKThread::GetStreamble().CreateAsyncRaw(this, FRMIProgress::CreateProgressUICompelte, ObjectPaths);*/
	}
}

void FRMIProgress::CreateProgressUICompelte(const TArray<FSoftObjectPath>& ObjectPaths)
{

}

bool FRMIProgress::IsLoadUIValid()
{
	if (m_pLoadUI && m_pLoadUI->IsValidLowLevel() && !m_pLoadUI->IsPendingKill())
		return true;
	return false;
}
